package com.dwcode.macro.service.impl;

import com.dwcode.macro.dto.Pagination;
import com.dwcode.macro.entity.TUser;
import com.dwcode.macro.dao.TUserDao;
import com.dwcode.macro.service.TUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TUser)表服务实现类
 *
 * @author makejava
 * @since 2020-09-17 22:04:57
 */
@Service("tUserService")
public class TUserServiceImpl implements TUserService {
    @Resource
    private TUserDao tUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TUser queryById(Integer id) {
        return this.tUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TUser> queryAllByLimit(int offset, int limit) {
        return this.tUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tUser 实例对象
     * @return 实例对象
     */
    @Override
    public TUser insert(TUser tUser) {
        this.tUserDao.insert(tUser);
        return tUser;
    }

    /**
     * 修改数据
     *
     * @param tUser 实例对象
     * @return 实例对象
     */
    @Override
    public TUser update(TUser tUser) {
        this.tUserDao.update(tUser);
        return this.queryById(tUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tUserDao.deleteById(id) > 0;
    }

    /**
     * 根据用户名和密码查询用户信息
     *
     * @param username
     * @param password
     */
    @Override
    public TUser findUserByNameAndPassword(String username, String password) {
        return tUserDao.queryByNameAndPassword(username,password);
    }

    /**
     * ajax获取数据
     * @param pagination
     * @return
     */
    @Override
    public Pagination<TUser> getUsers(Pagination pagination) {
        int start = (pagination.getPageNum() - 1) * pagination.getPageSize();
        int totalUser = tUserDao.countTotalUser();
        List<TUser> rows = tUserDao.getUsers(start, pagination.getPageSize());
        pagination.setTotal(totalUser);
        pagination.setRows(rows);
        return pagination;
    }

    /**
     * 根据条件查询查询
     */
     @Override
     public Pagination<TUser> findUserSearch(TUser user, Pagination<TUser> pagination){
         int start = (pagination.getPageNum() - 1) * pagination.getPageSize();

         List<TUser> tUsers = tUserDao.searchUser(user, start, pagination.getPageSize());
         pagination.setRows(tUsers);
         pagination.setTotal(tUserDao.searchCountUser(user));
         return pagination;
     }


    @Override
    public boolean deleteBatch(int[] list) {
        return tUserDao.deleteBatch(list);
    }

    @Override
    public List<TUser> queryAll(TUser user) {
        return tUserDao.queryAll(user);
    }
}